package com.nmu.app.Shrimp.veiw.javagame.dto;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import com.nmu.app.Shrimp.veiw.javagame.IVisableObject;
import com.nmu.app.Shrimp.veiw.javagame.util.KeyboardInput;
import com.nmu.app.Shrimp.veiw.javagame.util.MouseInput;
import com.nmu.app.Shrimp.veiw.javagame.util.Vector2f;

/**2d游戏框架中基础物体
 * 
 * @author hackM
 *
 */
public class Bale implements IVisableObject{
	public int positionX;
	public int positionY;
	public int objWidth;
	public int objHeigh;
	//选中点列表
	public List<Vector2f> selectedPoints=new ArrayList<Vector2f>();
	public float rotStep,rot;//旋转步长,初始角
	public Point centerPoint;
	public float outerRectR;
	public int degreed;
//	public float outerRectH;
	//状态标记对象
	public boolean objInMouseFocus=false;//焦点
	public boolean objInSelected=false;//选中
	
	
	
	public Bale(int positionX, int positionY, int objWidth, int objHeigh) {
		this.positionX = positionX;
		this.positionY = positionY;
		this.objWidth = objWidth;
		this.objHeigh = objHeigh;
		selectedPoints.add(new Vector2f(positionX, positionY));
		selectedPoints.add(new Vector2f(positionX+objWidth, positionY));
		selectedPoints.add(new Vector2f(positionX, positionY+objHeigh));
		selectedPoints.add(new Vector2f(positionX+objWidth, positionY+objHeigh));
		//间隔角
		degreed=360/selectedPoints.size();
		//各段弧角度=间隔角/4
		rot=0.0f;
		rotStep=(float) Math.toRadians(0.05);
		centerPoint=new Point(positionX+objWidth/2, positionY+objHeigh/2);
		outerRectR=(float) ((positionX-Math.sqrt(objWidth*objWidth*2))*0.6);
	}

	@Override
	public void render(Graphics g) {
		g.setColor(Color.RED);
		g.fillRect(positionX, positionY, objWidth, objHeigh);
//		g.fillOval(200, 200, 80, 80);
		
		//若处于标记状态,将对象显示标记
//		if(objInMouseFocus){
//			g.setColor(Color.GREEN);
//			g.drawRect(positionX-1, positionY-1, objWidth+2, objHeigh+2);
//		}
		//若对象处于选中,将对象高亮//temp
		if(objInMouseFocus){
			g.setColor(Color.BLACK);
//			for(Vector2f v:selectedPoints){
//				//圆形环绕
////				g.fillOval((int)v.x-(objWidth/10), (int)v.y-(objHeigh/10), objWidth/5, objHeigh/5);	
//				//圆弧环绕
//				
//			}
			for(int i=0;i<selectedPoints.size();i++){
				int a=i*degreed+(0-degreed/4);
				int b=i*degreed+degreed/4;
				int x=(int)(centerPoint.x-outerRectR/2);
				int y=(int)(centerPoint.y-outerRectR/2);
				//圆弧环绕
				g.drawArc(x, y, (int)outerRectR, (int)outerRectR, a, b);
			}
			
		}
	}

	@Override
	public void update(Point p) {
		//选中的对象,需要更新坐标显示点//temp
		if(objInMouseFocus){
			//匀变速
			rot+=rotStep;
			rot=(float) Math.toRadians(1);
			if(rot>2*Math.PI*0.05|| rot<0.0)rot=-rot;
			for(Vector2f v: selectedPoints){
				v.rotateRel(rot,centerPoint);
			}
			System.out.println("done");
//			sleep(20);
		}
		
		
	}

	@Override
	public void inputProcess(KeyboardInput ki) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseProcess(MouseInput mi) {
		//选择触发
		objectBeSelected(mi);
		
	}
	private void sleep(long sleep){
		try{
			Thread.sleep(sleep);
		}catch(Exception e){}
	}
	//目标被选中的触发
	public void objectBeSelected(MouseInput mi){
		if(mouseIntoObject(mi.getPosition())){
			objInMouseFocus=true;return;
		}
		if(!mouseIntoObject(mi.getLastPos())&&mouseIntoObject(mi.getPosition())){
			System.out.println("im in!");
			objInMouseFocus=true;
		}else{
			objInMouseFocus=false;
		}
	}
	
	//鼠标位于对象内
	public boolean mouseIntoObject(Point p){
		if(p.x>=positionX&&p.x<=positionX+objWidth
				&&p.y>=positionY&&p.y<=positionY+objHeigh)return true;
		return false;
	}

}
